/**
 * 
 */
package vehicleLogic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import vehicleEntity.RentalVehicleEntity;
import vehicleEntity.SaleVehicleEntity;
import vehicleService.ViewVehicleService;

/**
 * @author Yokesh Kumar
 * @author Sashiraj Dhanarajan
 *
 */
public class ViewVehicleLogic {

	/**
	 * This method fetches a list of vehicles available for sale
	 * @return
	 */
	public ObservableList<SaleVehicleEntity> fetchSaleList(){
		ResultSet saleListResultSet = null;
		ViewVehicleService viewService =  new ViewVehicleService();
		SaleVehicleEntity saleEntity ;
		ObservableList<SaleVehicleEntity> observableSaleList = null;
		List<SaleVehicleEntity> saleList = new ArrayList<SaleVehicleEntity>();
		try {
			saleListResultSet = viewService.fetchSaleList();
			if(saleListResultSet!= null){
				while (saleListResultSet.next()) {
					saleEntity = new SaleVehicleEntity();
					saleEntity.setSaleVehicleId(saleListResultSet.getInt("saleVehicleId"));
					saleEntity.setRegNo(saleListResultSet.getString("RegNo"));
					saleEntity.setVehicleType(saleListResultSet.getString("vehicletype"));
					saleEntity.setVehicleClass(saleListResultSet.getString("vehicleClass"));
					saleEntity.setVehicleName(saleListResultSet.getString("VehicleName"));
					saleEntity.setSaleAmount(saleListResultSet.getDouble("saleAmount"));
					saleList.add(saleEntity);
				}

				observableSaleList = FXCollections.observableList(saleList);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		viewService.closeConnection();
		return observableSaleList;
	}

	/**
	 * This method fetches the list of vehicle class based on vehicle type
	 * @param vehicleType
	 * @return Returns the vehicle types
	 * @pre The vehicle class shouls be selected
	 * @post Display the vehicle types based on the vehicle class selected
	 */
	public List<String> fetchVehicleType(String vehicleClass){
		ViewVehicleService service = new ViewVehicleService();
		ResultSet rs = null;
		List<String> vehicleTypeList = new ArrayList<String>();
		try {
			rs = service.fetchVehicleType(vehicleClass);
			if(rs !=null){
				while(rs.next()){
					String type = rs.getString("vehicleType");
					vehicleTypeList.add(type);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return vehicleTypeList;
	}

	/**
	 * This method fetches all the vehicles along with the status for the selected type
	 * @param from
	 * @return Returns vehicles with both available nad rented status
	 * @pre The vehicle class and vehicle type should be selected
	 * @post List the vehicle along with the status 
	 * 
	 */

	public ObservableList<RentalVehicleEntity> fetchAllVehicles(String from){
		ResultSet rs = null;
		ViewVehicleService service = new ViewVehicleService();
		ObservableList<RentalVehicleEntity> observableAllStatus = null;
		List<RentalVehicleEntity> allStatus = new ArrayList<RentalVehicleEntity>();
		RentalVehicleEntity status = null;
		try {
			rs = service.fetchAllVehicleStatus(from);
			if(rs != null){
				while(rs.next()){
					status = new RentalVehicleEntity();
					status.setVehicleId(rs.getInt("vehicleId"));
					status.setVehicleName(rs.getString("vehicleName"));
					status.setRentId(rs.getInt("rentId"));
					status.setStatus(rs.getString("status"));
					if(status.getRentId() != null && status.getRentId() ==0){
						status.setRentId(null);
					}
					allStatus.add(status);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		observableAllStatus = FXCollections.observableList(allStatus);
		service.closeConnection();
		return observableAllStatus;
	}

	/**
	 *  This method fetches all the vehicles along with the status for the selected type
	 * @param from
	 * @return Returns vehicles with both available and rented status
	 * @pre The vehicle class and vehicle type should be selected
	 * @post List the vehicle along with the status 
	 * 
	 */

	public ObservableList<RentalVehicleEntity> fetchVehiclesTypeBasedOnStatus(String from,String status){
		ResultSet rs = null;
		ViewVehicleService service = new ViewVehicleService();
		ObservableList<RentalVehicleEntity> observableAllStatus = null;
		List<RentalVehicleEntity> allStatus = new ArrayList<RentalVehicleEntity>();
		RentalVehicleEntity rentalVehicle = null;
		try {
			rs = service.fetchVehicleStatus(from,status);
			if(rs != null){
				while(rs.next()){
					rentalVehicle = new RentalVehicleEntity();
					rentalVehicle.setVehicleId(rs.getInt("vehicleId"));
					rentalVehicle.setVehicleName(rs.getString("vehicleName"));
					rentalVehicle.setRentId(rs.getInt("rentId"));
					rentalVehicle.setStatus(rs.getString("status"));
					if(rentalVehicle.getRentId() != null && rentalVehicle.getRentId() ==0){
						rentalVehicle.setRentId(null);
					}
					allStatus.add(rentalVehicle);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		observableAllStatus = FXCollections.observableList(allStatus);
		service.closeConnection();
		return observableAllStatus;
	}
	/**
	 * This method fetches vehicles based on the status selected
	 * @param VehicleClass
	 * @param status
	 * @return The vehicle with the status selected
	 * @pre The vehicle Class, vehicle Type and Vehicle Status should be selected
	 * @post List the vehicles based on the status selected
	 */

	public ObservableList<RentalVehicleEntity> fetchVehiclesBasedOnStatus(String VehicleClass, String status) {
		ResultSet rs = null;
		ViewVehicleService service = new ViewVehicleService();
		ObservableList<RentalVehicleEntity> observableAllStatus = null;
		List<RentalVehicleEntity> allStatus = new ArrayList<RentalVehicleEntity>();
		RentalVehicleEntity rentalVehicle = null;
		try {
			rs = service.fetchVehiclesBasedOnStatus(VehicleClass,status);
			if(rs != null){
				while(rs.next()){
					rentalVehicle = new RentalVehicleEntity();
					rentalVehicle.setVehicleId(rs.getInt("vehicleId"));
					rentalVehicle.setVehicleName(rs.getString("vehicleName"));
					rentalVehicle.setRentId(rs.getInt("rentId"));
					rentalVehicle.setStatus(rs.getString("status"));
					allStatus.add(rentalVehicle);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		observableAllStatus = FXCollections.observableList(allStatus);
		service.closeConnection();
		return observableAllStatus;
	}


}
